package com.example.demo.restcontroller;


import com.example.demo.entity.Authority;
import com.example.demo.service.AuthorityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

//解决跨域请求的问题
@CrossOrigin
@RestController
public class AuthorityManagementRestController {

    @Autowired
    AuthorityService authorityService;


    @GetMapping("/selectAllAuthorityListOrOne")
    public Map<String, Object> selectAllAuthorityListOrOne(Authority authority) {
        HashMap<String, Object> map = new HashMap<>();
        List<Authority> authorityList = authorityService.selectAllAuthorityListOrOne(authority);
        if (authorityList != null) {
            map.put("code", 0);
            map.put("msg", "权限列表");
            map.put("data", authorityList);
        } else map.put("code", -1);
        return map;
    }

    @PostMapping("/insertAuthority")
    public Map<String, Object> insertAuthority(Authority authority) {
        HashMap<String, Object> map = new HashMap<>();
        Integer count = authorityService.getAllAuthorityListCount();
        authority.setId((count)+1);
        int i = authorityService.insertAuthority(authority);
        if (i > 0) {
            map.put("code", 200);
        } else map.put("code", 200);
        return map;
    }

}
